<?php
/**
 * Class Person
 *
 *
 * @name person.class.php
 * @author
 * @copyright
 * @version

 */


Class Person {

	/**
	 *	Propriétés de la classe
	 */
	private 	$idPerson;
	private 	$nomPerson;
	private 	$prenomPerson;
	private 	$dateNaissancePerson;
	private 	$adressePerson;
	private 	$telPerson;
	private 	$emailPerson;
	private 	$rolePerson;
	private 	$passwordPerson;
	private 	$keyPerson;
	private 	$idCity;
	private 	$monCoffre;
	private 	$mesEchanges;




	/**
	 *	Méthodes de la classe
	 */

	public function __construct(){
			
	}


	/* Initialiser */
	public function initialisation(
		$p_idPerson,
		$p_nomPerson,
		$p_prenomPerson,
		$p_dateNaissancePerson,
		$p_adressePerson,
		$p_telPerson,
		$p_emailPerson,
		$p_rolePerson,
		$p_passwordPerson,
		$p_keyPerson,
		$p_idCity
	){

		$this->idPerson = $p_idPerson;
		$this->nomPerson = $p_nomPerson;
		$this->prenomPerson = $p_prenomPerson;
		$this->dateNaissancePerson = $p_dateNaissancePerson;
		$this->adressePerson = $p_adressePerson;
		$this->telPerson = $p_telPerson;
		$this->emailPerson = $p_emailPerson;
		$this->rolePerson = $p_rolePerson;
		$this->passwordPerson = $p_passwordPerson;
		$this->keyPerson = $p_keyPerson;
		$this->idCity = $p_idCity;
		$this->chargerMonCoffre();
		$this->chargerMesEchanges();

	}
	
	
	/* Getteur */

	public function getIdPerson() {
		return $this->idPerson;
	}
	public function getNomPerson() {
		return $this->nomPerson;
	}
	public function getPrenomPerson() {
		return $this->prenomPerson;
	}
	public function getDateNaissancePerson() {
		return $this->dateNaissancePerson;
	}
	public function getAdressePerson() {
		return $this->adressePerson;
	}
	public function getTelPerson() {
		return $this->telPerson;
	}
	public function getEmailPerson() {
		return $this->emailPerson;
	}
	public function getRolePerson() {
		return $this->rolePerson;
	}
	public function getPasswordPerson() {
		return $this->passwordPerson;
	}
	public function getKeyPerson() {
		return $this->keyPerson;
	}
	public function getIdCity() {
		return $this->idCity;
	}
	public function getMonCoffre() {
		return $this->monCoffre;
	}
	public function getMesEchanges() {
		return $this->mesEchanges;
	}
	
	
	
	/* Setteur */

	public function setIdPerson($p_IdPerson) {
		$this->idPerson = $p_IdPerson;
	}
	public function setNomPerson($p_nomPerson) {
		$this->nomPerson = $p_nomPerson;
	}
	public function setPrenomPerson($p_prenomPerson) {
		$this->prenomPerson = $p_prenomPerson;
	}
	public function setDateNaissancePerson($p_dateNaissancePerson) {
		$this->dateNaissancePerson = $p_dateNaissancePerson;
	}
	public function setAdressePerson($p_adressePerson) {
		$this->adressePerson = $p_adressePerson;
	}
	public function setTelPerson($p_telPerson) {
		$this->telPerson = $p_telPerson;
	}
	public function setEmailPerson($p_emailPerson) {
		$this->emailPerson = $p_emailPerson;
	}
	public function setRolePerson($p_rolePerson) {
		$this->rolePerson = $p_rolePerson;
	}
	public function setPasswordPerson($p_PasswordPerson) {
		$this->passwordPerson = $p_PasswordPerson;
	}	
	public function setKeyPerson($p_keyPerson) {
		$this->keyPerson = $p_keyPerson;
	}
	public function setIdCity($p_idCity) {
		$this->idCity = $p_idCity;
	}
	
	
	
	
	// Supprimer la personne dans la BDD
	public function remove() {
		$sql = BDD::getInstance();
		$result = $sql->query("DELETE FROM `Person` WHERE idPerson=$this->idPerson");
	}
	
	
	
	// Ajoute la personne dans la BDD
	public function add() {
		$sql = BDD::getInstance();
		/*
		* Utilisation de $sql->getPdo()->quote() pour la sŽcuritŽ
		* concernant les carat�res spŽciaux et autres, cela Žvite
		* les vŽrifications avant l'appel de add();
		*/
		$nom = utf8_decode($this->nomPerson);
		$prenom = utf8_decode($this->prenomPerson);
		$adresse = utf8_decode($this->adressePerson);		
		$key = strtoupper(utf8_decode($this->keyPerson));
		
		$req = "INSERT INTO `Person` VALUES(
											'',
											'$nom',
											'$prenom',
											'$this->dateNaissancePerson', 
											'$adresse', 
											'$this->telPerson', 
											'$this->emailPerson', 
											$this->rolePerson, 
											'$this->passwordPerson', 
											'$key', 
											$this->idCity
											)";
		$result = $sql->exec($req);
		

		return $result;
	}	
	
	
	
	
	// Met ˆ jour la personne dans la BDD
	public function update() {
		$sql = BDD::getInstance();
		
		$req = "UPDATE `Person` SET 
												nomPerson='$this->nomPerson',
												prenomPerson='$this->prenomPerson', 
												dateNaissancePerson='$this->dateNaissancePerson', 
												adressePerson='$this->adressePerson', 
												telPerson='$this->telPerson', 
												emailPerson='$this->emailPerson', 
												passwordPerson='$this->passwordPerson', 
												\$idCity=$this->idCity 
		
								WHERE idPerson=$this->idPerson";
		
		$result = $sql->exec($req);
		return $req;
	}
	
	
	
	
	// Charge l'Array d'ID de Group
	public function chargerMonCoffre() {
		$sql = BDD::getInstance();
		$this->monCoffre = Array();
		$result = $sql->query("SELECT * FROM `Object` WHERE \$idPerson = $this->idPerson");
		
		foreach ($result AS $key => $theObject) {
			$this->monCoffre[$key] = $theObject["idObject"];  // Ajoute l'ID de l'Objet dans le Array()
		}
	}
	
	
	

	// Charge l'Array d'ID de Echange
	public function chargerMesEchanges() {
		$sql = BDD::getInstance();
		$this->mesEchanges = Array();
		$result = $sql->query("
							   SELECT idExchange 
							   FROM  `Exchange` 
							   WHERE \$idPerson1 = $this->idPerson 
							   OR
							   \$idPerson2 = $this->idPerson
						     ");
		
		foreach ($result AS $key => $lEchange) {
			$this->mesEchanges[$key] = $lEchange["idExchange"];  // Ajoute l'ID de l'Exchange dans le Array()
		}	
	}

	
	
	

};
?>